
// 全局变量
var areaNum = 100001;
var tblType = 0;
var saleAttr = 99;
var dataType = 0;
var contentData = [];
var contentTitle = -1;

var mdata = {
        
    // 封装获取Jsgx Mobile Data 的 URL
    URL : {
        jsgxMobileData : function(typeId, saleAttr) {
            return '/wexin_info/' + saleAttr + '/' + typeId + '/getData';
        },
    },
    
    // 格式化数字
    format : {
        number : function (number, decimals, dec_point, thousands_sep) {
             /*
             3     * 参数说明：
             4     * number：要格式化的数字
             5     * decimals：保留几位小数
             6     * dec_point：小数点符号
             7     * thousands_sep：千分位符号
             8     * */
             number = (number + '').replace(/[^0-9+-Ee.]/g, '');
             var n = !isFinite(+number) ? 0 : +number,
             prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
             sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
             dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
             s = '',
             toFixedFix = function (n, prec) {
                 var k = Math.pow(10, prec);
                return '' + Math.ceil(n * k) / k;
             };
             
             s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
             var re = /(-?\d+)(\d{3})/;
             while (re.test(s[0])) {
                s[0] = s[0].replace(re, "$1" + sep + "$2");
             }
             
             if ((s[1] || '').length < prec) {
                 s[1] = s[1] || '';
                 s[1] += new Array(prec - s[1].length + 1).join('0');
             }
             return s.join(dec);
        }
    },

    // 获取数据逻辑
    detail : {
        getData : function (params) {
            var typeId = params['typeId'];
            var saleAttr = params['saleAttr'];
            var tblType = params['type'];
            var dataType = params['dataType'];
            var panelTitle = params['panelTitle'];
            $.get(mdata.URL.jsgxMobileData(typeId, saleAttr), {}, function(result) {
                if (result && result['code']) {
                    dataContent = result['data']['list'];
                    mdata.content.create(dataContent, tblType, dataType, panelTitle);
                } else {
                }
            })
        }
    },
    
    // 创建 html 列表
    content : {
        create : function (data, tblType, detail, panelTitle) {
            var content = $('#panel-content');
            content.empty();
            

            if (tblType == 0) {
                var _html = `<ul class="list-group">`;
                //json 遍历
                for (ele in data) {
                    var yearRate = mdata.format.number(data[ele].yearRate, 2, ".", ",");
                    var monRate = mdata.format.number(data[ele].monRate, 2, ".", ",");
                    var dayBzamt = mdata.format.number(data[ele].dayBzamt, 2, ".", ",");
                    var dayAmt = mdata.format.number(data[ele].dayAmt, 2, ".", ",");
                    var monBzamt = mdata.format.number(data[ele].monBzamt, 2, ".", ",");
                    var monBzamtRate = mdata.format.number(data[ele].monBzamtRate, 2, ".", ",");
                    var monAmt = mdata.format.number(data[ele].monAmt, 2, ".", ",");
                    var monAmtRate = mdata.format.number(data[ele].monAmtRate, 2, ".", ",");
                    var yearBzamt = mdata.format.number(data[ele].yearBzamt, 2, ".", ",");
                    var yearBzamtRate = mdata.format.number(data[ele].yearBzamtRate, 2, ".", ",");
                    var yearAmt = mdata.format.number(data[ele].yearAmt, 2, ".", ",");
                    var yearAmtRate = mdata.format.number(data[ele].yearAmtRate, 2, ".", ",");
                
                    _html = `<li class="list-group-item"><span class="badge">${yearRate}%</span>本年标保达成率</li>
                                 <li class="list-group-item"><span class="badge">${monRate}%</span>本月标保达成率</li>
                                 <li class="list-group-item"><span class="badge">${dayBzamt}</span>本日标保</li>
                                 <li class="list-group-item"><span class="badge">${dayAmt}</span>本日新保</li>
                                 <li class="list-group-item"><span class="badge">${monBzamt}/${monBzamtRate}%</span>本月标保/同比</li>
                                 <li class="list-group-item"><span class="badge">${monAmt}/${monAmtRate}%</span>本月新保/同比</li>
                                 <li class="list-group-item"><span class="badge">${yearBzamt}/${yearBzamtRate}%</span>本年标保/同比</li>
                                 <li class="list-group-item"><span class="badge">${yearAmt}/${yearAmtRate}%</span>本年新保/同比</li>`;
                }
                
                _html = _html + "</ul>";
            } else {
	    		var _html = `<div class="table-responsive"><table class="table">`;
	        
	    		//json 遍历
	            if (panelTitle === 8) {
	                //for (ele in data) {
	                    if (detail == 0) {
	
	                        _html = _html + `<thead><tr><td>机构</td><td>每日标保</td><td>每日新保</td></tr></thead>`;
	                        _html = _html + `<tbody>`;
	                        for (ele in data) {
	                        	var dayBzamt = mdata.format.number(data[ele].dayBzamt, 2, ".", ",");
	                            var dayAmt = mdata.format.number(data[ele].dayAmt, 2, ".", ",");
	                            _html = _html + `<tr><td>${data[ele].name}</td><td>${dayBzamt}</td><td>${dayAmt}</td></tr>`;
	                        }
	                        _html = _html + `</tbody>`;
	                        
	                    } else if (detail == 1) {
	                    	
	                    	_html = _html + `<thead><tr><td>机构</td><td>本月标保</td><td>本月标保达成率</td><td>本月标保同比</td></tr></thead>`;
	                        _html = _html + `<tbody>`;
	                        for (ele in data) {
	                        	var monRate = mdata.format.number(data[ele].monRate, 2, ".", ",");
	                            var monBzamt = mdata.format.number(data[ele].monBzamt, 2, ".", ",");
	                            var monBzamtRate = mdata.format.number(data[ele].monBzamtRate, 2, ".", ",");
	                            _html = _html + `<tr><td>${data[ele].name}</td><td>${monBzamt}</td><td>${monRate}</td><td>${monBzamtRate}</td></tr>`;
	                        }
	                        _html = _html + `</tbody>`;
	
	                    } else if (detail == 2) {
	                    	
	                    	_html = _html + `<thead><tr><td>机构</td><td>本年标保</td><td>本年标保达成率</td><td>本年标保同比</td></tr></thead>`;
	                        _html = _html + `<tbody>`;
	                        for (ele in data) {
	                        	var yearRate = mdata.format.number(data[ele].yearRate, 2, ".", ",");
	                            var yearBzamt = mdata.format.number(data[ele].yearBzamt, 2, ".", ",");
	                            var yearBzamtRate = mdata.format.number(data[ele].yearBzamtRate, 2, ".", ",");
	                            _html = _html + `<tr><td>${data[ele].name}</td><td>${yearBzamt}</td><td>${yearRate}</td><td>${yearBzamtRate}</td></tr>`;
	                        }
	                        _html = _html + `</tbody>`;
	
	                        <!-- _html = _html + `<li class="list-group-item"><span class="badge">${yearBzamt}/${yearRate}/${yearBzamtRate}</span>${data[ele].name}</li>`; -->
	                    }
	            } else if (panelTitle === 1) {
	            	
	            	_html = _html + `<thead><tr><td>机构</td><td>本月健康人力</td></tr></thead>`;
	                _html = _html + `<tbody>`;
	                for (ele in data) {
	                	var empMonJy = mdata.format.number(data[ele].empMonJy, 2, ".", ",");
	                    _html = _html + `<tr><td>${data[ele].name}</td><td>${empMonJy}</td></tr>`;
	                }
	                _html = _html + `</tbody>`;
	                
	            } else if (panelTitle === 2) {
	            	
	            	_html = _html + `<thead><tr><td>机构</td><td>本月健康人力</td></tr></thead>`;
	                _html = _html + `<tbody>`;
	                for (ele in data) {
	                	var empMonJy = data[ele].empMonJk;
	                    _html = _html + `<tr><td>${data[ele].name}</td><td>${empMonJk}</td></tr>`;
	                }
	                _html = _html + `</tbody>`;
	                
	            } else if (panelTitle === 3) {
	            	
	            	_html = _html + `<thead><tr><td>机构</td><td>本月绩优人力</td></tr></thead>`;
	                _html = _html + `<tbody>`;
	                for (ele in data) {
	                	var empMonJy = data[ele].empMonJy;
	                    _html = _html + `<tr><td>${data[ele].name}</td><td>${empMonJy}</td></tr>`;
	                }
	                _html = _html + `</tbody>`;
	                
	            } else if (panelTitle === 4) {
	
	            	_html = _html + `<thead><tr><td>机构</td><td>本月健康人力</td></tr></thead>`;
	                _html = _html + `<tbody>`;
	                for (ele in data) {
	                	var empMonJy = mdata.format.number(data[ele].empMonJy, 2, ".", ",");
	                    _html = _html + `<tr><td>${data[ele].name}</td><td>${empMonJy}</td></tr>`;
	                }
	                _html = _html + `</tbody>`;
	                
	            } else if (panelTitle === 5) {
	            	
	            	_html = _html + `<thead><tr><td>机构</td><td>本月健康人力</td></tr></thead>`;
	                _html = _html + `<tbody>`;
	                for (ele in data) {
	                	var empMonJy = mdata.format.number(data[ele].empMonJy, 2, ".", ",");
	                    _html = _html + `<tr><td>${data[ele].name}</td><td>${empMonJy}</td></tr>`;
	                }
	                _html = _html + `</tbody>`;
	                
	            } else if (panelTitle === 6) {
	
	            	_html = _html + `<thead><tr><td>机构</td><td>本月健康人力</td></tr></thead>`;
	                _html = _html + `<tbody>`;
	                for (ele in data) {
	                	var empMonJy = mdata.format.number(data[ele].empMonJy, 2, ".", ",");
	                    _html = _html + `<tr><td>${data[ele].name}</td><td>${empMonJy}</td></tr>`;
	                }
	                _html = _html + `</tbody>`;
	                
	            } else if (panelTitle === 7) {
	
	            	_html = _html + `<thead><tr><td>机构</td><td>本月健康人力</td></tr></thead>`;
	                _html = _html + `<tbody>`;
	                for (ele in data) {
	                	var empMonJy = mdata.format.number(data[ele].empMonJy, 2, ".", ",");
	                    _html = _html + `<tr><td>${data[ele].name}</td><td>${empMonJy}</td></tr>`;
	                }
	                _html = _html + `</tbody>`;
	                
	            } else if (panelTitle === 8) {
	            	
	            	_html = _html + `<thead><tr><td>机构</td><td>本月健康人力</td></tr></thead>`;
	                _html = _html + `<tbody>`;
	                for (ele in data) {
	                	var empMonJy = mdata.format.number(data[ele].empMonJy, 2, ".", ",");
	                    _html = _html + `<tr><td>${data[ele].name}</td><td>${empMonJy}</td></tr>`;
	                }
	                _html = _html + `</tbody>`;
	                
	            }
	            
	            _html = _html + "</table></div>";
	        }
	        content.html(_html);
        },
        
        addTd : function(params) {
            var content = $("#" + params['id']);
            content.empty();
            
            var _html = "<span>" + params['content'] + "</span>";
            
            content.html(_html);
        },
        
        addDateBtn : function(params) {
            var id = $("#" + params['id']);
            id.empty();
            var content = params['content'];
            $.each(content, function(i, item){
                if (item === 1) {
                    if (i === 0) {
                        var _html = `<button type="button" class="btn btn-default btn-lg" id="dayBtn">日级数据</button>`;
                        id.append(_html);
                        document.getElementById("dayBtn").onclick = function(){
                            dataType = 0;
                            // 向 表格添加 已选项
                            mdata.content.addTd({
                                id        : "date-td",
                                content   : $("#dayBtn").text()
                            })
                        };
                    }
                    if (i === 1) {
                        var _html = `<button type="button" class="btn btn-default btn-lg" id="monBtn">月份数据</button>`;
                        id.append(_html);
                        document.getElementById("monBtn").onclick = function(){
                            dataType = 1;
                            // 向 表格添加 已选项
                            mdata.content.addTd({
                                id        : "date-td",
                                content   : $("#monBtn").text()
                            })   
                        };
                    }
                    if (i === 2) {
                        var _html = `<button type="button" class="btn btn-default btn-lg" id="yeaBtn">年度数据</button>`;
                        id.append(_html);
                        document.getElementById("yeaBtn").onclick = function(){
                            dataType = 2;
                            // 向 表格添加 已选项
                            mdata.content.addTd({
                                id        : "date-td",
                                content   : $("#yeaBtn").text()
                            })
                        };
                    }
                }
            });
        },
        
        addTitle : function(params) {
            var id = $("#" + params['id']);
            var title = params['content'];
            id.addClass("active");
            if (title === 0) {
                id.text("出勤人力");
            } else if (title === 1) {
                if (dataType === 0) {
                    id.text("本日长线千P举债人力");
                } else if (dataType === 1) {
                    id.text("本月长线千P举债人力");
                }
            } else if (title === 2) {
                id.text("本月健康人力");
            } else if (title === 3){
                id.text("本月绩优人力");
            } else if (title === 4) {
                id.text("标保达成率排名");
            } else if (title === 5) {
                id.text("标保同比排名");
            } else if (title === 6) {
                id.text("标保规模排名");
            } else if (title === 7) {
                id.text("新保期缴同比排名");
            } else if (title === 8) {
                id.text("新保规模排名");
            }
        }
    },
    
}